<template>
  <div id="bigbox">
    <!-- 登陆框 -->
    <div class="box">
      <!-- 头像 -->
      <div class="buddha">
        <img src="../assets/logo.png" alt="" />
      </div>
      <!-- 表单 -->
      <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" class="xbox">
        <el-form-item prop="username">
          <el-input
            v-model="ruleForm.username"
            prefix-icon="el-icon-user-solid"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            type="password"
            v-model="ruleForm.password"
            prefix-icon="el-icon-s-goods"
          ></el-input>
        </el-form-item>
        <el-form-item class="button">
          <el-button type="primary" @click="submitForm('ruleForm')">登陆</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  components: {},
  name: "",
  data() {
    return {
      ruleForm: {
        username: "",
        password: "",
      },
      rules: {
        username: [
          { required: true, message: "请填写用户名" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请填写密码" },
          { min: 3, max: 8, message: "长度在 3 到 8 个字符", trigger: "blur" },
        ],
      },
    };
  },
  mounted() {},
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          //验证成功获取接口数据
          this.$axios.post("login", this.ruleForm).then((res) => {
            console.log(res);
            if (res.meta.status == 200) {
              //保存token
              window.sessionStorage.setItem("lc_05B", res.data.token);
              //跳转页面
              this.$router.push("/");
              //提示登录成功
              this.$message.success(res.meta.msg);
            }else{
              this.$message.error(res.meta.msg)
            }
          });
        } else {
          return false;
        }
      });
    },

    //重置账号密码
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
  computed: {},
  watch: {},
};
</script>

<style lang="scss" scoped>
#bigbox {
  background-color: #2b446b;
  height: 100%;
  .box {
    width: 450px;
    height: 300px;
    background-color: white;
    box-shadow: 0 0 10px #ddd;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    .buddha {
      width: 150px;
      height: 150px;
      background-color: white;
      box-shadow: 0 0 10px rgb(161, 156, 156);
      border-radius: 50%;
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);
      border: 1px solid #eee;
      padding: 12px;
      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background-color: #eee;
      }
    }

    .xbox {
      position: absolute;
      bottom: 0;
      left: 0;
      width: 100%;
      padding: 0 20px;
      box-sizing: border-box;
      .button {
        display: flex;
        justify-content: flex-end;
        .el-button:nth-child(2) {
          background-color: rgb(68, 65, 65);
          color: white;
        }
      }
    }
  }
}
</style>
